package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏西西
 * Date: 2023-08-28
 * Time: 14:20
 */
public class ThreadDemo12 {
    public static void main(String[] args) {
        //假设当前有两个线程，需要把两个变量各自自增 1000w次（典型的CPU密集型的场景）
        //可以一个线程，先针对a自增，然后再针对b自增
        //还可以两个线程，分别针对a,b自增
        serial();
        //current();
    }
    //串行执行，一个线程完成
    public static void serial(){

        //为了衡量执行的速度，加上一个计时器
        //currentTimeMillis(); 获取当前系统的ms级时间戳
        long beg = System.currentTimeMillis();
        long a = 0;
        for (long i = 0; i < 100_0000_0000L; i++) {
            a++;
        }
        long b = 0;
        for (long i = 0; i < 100_0000_0000L; i++) {
            b++;
        }

        long end = System.currentTimeMillis();
        System.out.println("执行时间" +(end - beg)+ "ms");
    }
    public static void current(){
        Thread t1 = new Thread(() ->{
            long a = 0;
            for (long i = 0; i < 100_0000_0000L; i++) {
                a++;
            }
        });
        Thread t2 = new Thread(() ->{
            long b = 0;
            for (long i = 0; i < 100_0000_0000L; i++) {
                b++;
            }
        });
        //开始计时
        long beg = System.currentTimeMillis();
        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //结束计时
        long end = System.currentTimeMillis();
        System.out.println("并发执行时间" +(end - beg) + " ms");
    }
}
